import requests
from PySide6.QtCore import QThread, Signal
from settings import HOST


class ChatroomMembersThread(QThread):
    error_signal = Signal(str)
    success_signal = Signal(dict)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.chatroom = None

    def set_chatroom(self, chatroom_id):
        self.chatroom = chatroom_id

    def run(self):
        if not self.chatroom:
            return
        try:
            r = requests.post(HOST, json={'type': 6, 'chat_room_id': self.chatroom})
            data = r.json()
            if data['errno'] == 0:
                self.success_signal.emit(data['data'])
        except Exception as e:
            self.error_signal.emit(str(e))
